package cn.com.gdut.ppj.comtroller;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.gdut.ppj.common.utils.UUIDFactory;
import cn.com.gdut.ppj.hr.service.IHrService;
import cn.com.gdut.ppj.model.TbCandidateRecruitmentInfo;
import cn.com.gdut.ppj.model.TbCompany;
import cn.com.gdut.ppj.model.TbHr;
import cn.com.gdut.ppj.model.TbRecruitmentInformation;
import cn.com.gdut.ppj.model.TbUser;
import cn.com.gdut.ppj.user.service.IUserService;
import cn.com.gdut.ppj.vo.HrResumeViewVO;

@Controller
@RequestMapping(value="/hr")
public class HrController {
	
	@Resource private IUserService userService;
	@Resource private IHrService hrService;

	@RequestMapping(value={"/updatecri"},method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView updatecri(HttpSession session,
			String isPass,
			String id
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbCandidateRecruitmentInfo cri = new TbCandidateRecruitmentInfo();//求职状态
		if(null!=isPass && ""!=isPass) cri.setIsPass(isPass);
		cri.setIsRead("yes");
		cri.setCriId(id);
		try {
			if(null != user){
				cri = hrService.updateCandidateRecruitmentInfo(cri);
				mav.addObject("code","0");
				return mav;
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value={"/resumesInfo"},method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView resumesInfo(HttpSession session,
			String isRead,
			String isPass
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbCandidateRecruitmentInfo cri = new TbCandidateRecruitmentInfo();//求职状态
		if(null!=isPass && ""!=isPass) cri.setIsPass(isPass);
		if(null!=isRead && ""!=isRead) cri.setIsRead(isRead);
		
		try {
			if(null != user){
				cri.setUserId(user.getUserId());
				List<HrResumeViewVO> list = hrService.hrResumeView(cri);
				
				mav.addObject("code","0");
				mav.addObject("list",list);
				return mav;
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value="/getPositionById",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView getPositionById(HttpSession session,
			String id
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setRiId(id);
		
		try {
			if(null != user){
				List<TbRecruitmentInformation> list = hrService.selectRecruitmentInformation(tri);
				if(null != list && !list.isEmpty()){
					mav.addObject("code","0");
					mav.addObject("tri",list.get(0));
					mav.addObject("msg","操作成功");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","操作失败，请刷新页面后再试");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value="/positionOn",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView positionOn(HttpSession session,
			String id
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setRiId(id);
		tri.setStatus("on");
		
		try {
			if(null != user){
				tri = hrService.updateRecruitmentInformation(tri);
				if(null != tri){
					mav.addObject("code","0");
					mav.addObject("msg","操作成功");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","操作失败，请刷新页面后再试");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value="/positionOff",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView positionOff(HttpSession session,
			String id
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setRiId(id);
		tri.setStatus("off");
		
		try {
			if(null != user){
				
				tri = hrService.updateRecruitmentInformation(tri);
				if(null != tri){
					mav.addObject("code","0");
					mav.addObject("msg","操作成功");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","操作失败，请刷新页面后再试");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value="/updatePosition",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView updatePosition(HttpSession session,
			String positionType,
			String positionName,
			String salaryMin,
			String salaryMax,
			String workCity,
			String experience,
			String education,
			String benefits,
			String positionDetail,
			String workAddress,
			String id
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setRiId(id);
		tri.setPositionTypeDescs(positionType);
		tri.setPositionDesc(positionName);
		tri.setSalaryMin(salaryMin);
		tri.setSalaryMax(salaryMax);
		tri.setWorkCity(workCity);
		tri.setDemandExpDesc(experience);
		tri.setEducationalBackgroundDesc(education);
		tri.setBenefits(benefits);
		tri.setDescription(positionDetail);
		tri.setWorkAddress(workAddress);
		tri.setPublishTime(Calendar.getInstance().getTime());
		tri.setStatus("on");
		
		try {
			if(null != user){
				tri = hrService.updateRecruitmentInformation(tri);
				if(null != tri){
					mav.addObject("code","0");
					mav.addObject("msg","更新成功");
					mav.addObject("href","hr/cp/result");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","招聘信息不存在");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
		
	}
	
	@RequestMapping(value="/getPositions",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView createPosition(HttpSession session,
			String status
			){
		
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setStatus(status);
		
		try {
			if(null != user){
				tri.setHr(user.getUserId());
				List<TbRecruitmentInformation> list = hrService.selectRecruitmentInformation(tri);
				mav.addObject("code","0");
				mav.addObject("list",list);
				mav.addObject("msg","获取信息成功");
				return mav;
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	@RequestMapping(value="/createPosition",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView createPosition(HttpSession session,
			String positionType,
			String positionName,
			String salaryMin,
			String salaryMax,
			String workCity,
			String experience,
			String education,
			String benefits,
			String positionDetail,
			String workAddress
			){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		
		tri.setRiId(UUIDFactory.getUUID());
		tri.setPositionTypeDescs(positionType);
		tri.setPositionDesc(positionName);
		tri.setSalaryMin(salaryMin);
		tri.setSalaryMax(salaryMax);
		tri.setWorkCity(workCity);
		tri.setDemandExpDesc(experience);
		tri.setEducationalBackgroundDesc(education);
		tri.setBenefits(benefits);
		tri.setDescription(positionDetail);
		tri.setWorkAddress(workAddress);
		tri.setPublishTime(Calendar.getInstance().getTime());
		
		try {
			if(null != user){
				tri.setHr(user.getUserId());
				tri = hrService.addRecruitmentInformation(tri, user);
				if(null != tri){
					mav.addObject("code","0");
					mav.addObject("msg","更新成功");
					mav.addObject("href","hr/cp/result");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","公司信息不存在");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
		
	}
	
	@RequestMapping(value="/updateCompany",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView updateCompany(HttpSession session,
			String name ,String city,String welfare,
			String industry,String scale,String temptation,
			String companyId){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		
		TbCompany company = new TbCompany();
		company.setCompanyId(companyId);
		company.setCity(city);
		company.setCompanyName(name);
		company.setInfo(temptation);
		company.setWelfare(welfare);
		company.setIndusty(industry);
		company.setScale(scale);
		
		try {
			if(null != user){
				company = hrService.updateCompany(company);
				if(null != company){
					mav.addObject("code","0");
					mav.addObject("msg","更新成功");
					mav.addObject("href","hr/myhome");
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","公司信息不存在");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
	
	
	@RequestMapping(value="/getCompany",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView createCompany(HttpSession session){
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		TbCompany company = new TbCompany();
		try {
			if(null != user){
				company.setHrs(user.getUserId());
				company = hrService.selectCompanyByHrId(company);
				if(null != company){
					mav.addObject("code","0");
					mav.addObject("msg","获取信息成功");
					mav.addObject("company",company);
					return mav;
				}else{
					mav.addObject("code","1001");
					mav.addObject("msg","公司信息不存在");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}
			
			
	@RequestMapping(value="/createCompany",method=RequestMethod.POST)
	@ResponseBody
	public ModelAndView createCompany(
			HttpSession session,
			String name ,String city,String welfare,
			String industy,String scale,String temptation
			){
		
		ModelAndView mav = new ModelAndView();
		TbUser user = (TbUser) session.getAttribute("user");
		
		TbCompany company = new TbCompany();
		company.setCompanyId(UUIDFactory.getUUID());
		company.setCity(city);
		company.setCompanyName(name);
		company.setInfo(temptation);
		company.setWelfare(welfare);
		company.setIndusty(industy);
		company.setScale(scale);
		
		TbHr hr = new TbHr();
		hr.setCompanyId(company.getCompanyId());
		hr.setId(UUIDFactory.getUUID());
		hr.setStartTime(Calendar.getInstance().getTime());
		
		try {
			if(null != user){
				company.setHrs(user.getUserId());
				hr.setUserId(user.getUserId());
				if(null != hrService.selectHrByUserId(hr)){
					mav.addObject("code","1001");
					mav.addObject("msg","当前账号已经存在公司，无法再创建");
					return mav;
				}else{
					hrService.addHrAndComapny(hr, company);
					mav.addObject("code","0");
					mav.addObject("msg","注册公司成功");
					mav.addObject("href","common/createSuccess");
					return mav;
				}
			}else{
				mav.addObject("code","1002");
				mav.addObject("msg","请先登录");
				return mav;
			}
		} catch (Exception e) {
			mav.addObject("code","1003");
			mav.addObject("msg","系统异常，请稍后再试");
			e.printStackTrace();
		}
		return mav;
	}

	@RequestMapping("/myhome")
	public String createSuccess(HttpServletRequest request, Model model){
		System.out.println("validate fo hr my home page");
		return "hr/myhome";
	}
	@RequestMapping("/updateCompany")
	public String updateCompanyUrl(HttpServletRequest request, Model model){
		System.out.println("validate fo hr company update page");
		return "hr/company/updateCompany";
	}
	@RequestMapping("/position")
	public String positions(HttpServletRequest request, Model model){
		System.out.println("validate fo hr positions page");
		return "hr/position/positions";
	}
	@RequestMapping("/position/stop")
	public String stopPositions(HttpServletRequest request, Model model){
		System.out.println("validate fo hr positions page");
		return "hr/position/stopPositions";
	}
	@RequestMapping("/position/create")
	public String positionCreate(HttpServletRequest request, Model model){
		System.out.println("validate fo hr positions page");
		return "hr/createPositions/create";
	}
	
	@RequestMapping("/cp/result")
	public String createPositionResult(HttpServletRequest request, Model model){
		System.out.println("validate fo create position success page");
		return "hr/createPositions/result";
	}
	
	@RequestMapping("/updatepsw")
	public String updatepsw(HttpServletRequest request, Model model){
		System.out.println("validate fo update personal password page");
		return "hr/personal/updatePwd";
	}
	
	@RequestMapping("/position/list")
	public String positionList(HttpServletRequest request, Model model){
		System.out.println("validate fo update personal password page");
		return "hr/position/list";
	}
	
	@RequestMapping("/resumes/canInte")
	public String resumes(HttpServletRequest request, Model model){
		System.out.println("validate fo resumes page");
		return "hr/resumes/canInte";
	}
	@RequestMapping("/resumes/pass")
	public String passResumes(HttpServletRequest request, Model model){
		System.out.println("validate fo resumes page");
		return "hr/resumes/pass";
	}
	@RequestMapping("/resumes/nopass")
	public String noPassResumes(HttpServletRequest request, Model model){
		System.out.println("validate fo resumes page");
		return "hr/resumes/nopass";
	}
	
	@RequestMapping("/positionWriter/{id}")
	public String positionWriter(HttpServletRequest request, Model model,
			@PathVariable("id") String id) throws Exception{
		System.out.println("validate fo position writer page and id = " + id);
		request.setAttribute("id", id);
		TbRecruitmentInformation tri = new TbRecruitmentInformation();
		tri.setRiId(id);
		List<TbRecruitmentInformation> list = hrService.selectRecruitmentInformation(tri);
		if(null != list && !list.isEmpty()){
			request.setAttribute("tri",list.get(0));
		}
		return "hr/createPositions/writer";
	}
}
